package com.zoweunion.mechanic.service.iot.impl;


import com.zoweunion.mechanic.dao.iot.AppManagementDao;
import com.zoweunion.mechanic.model.User;
import com.zoweunion.mechanic.service.base.BaseService;
import com.zoweunion.mechanic.service.iot.AppManagementService;
import com.zoweunion.mechanic.util.MyException;
import com.zoweunion.mechanic.util.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AppManagementServiceImpl extends BaseService implements AppManagementService {

    @Autowired
    private AppManagementDao appManagementDao;

    @Override
    public Map<String, Object> pictureList(User user, Map<String, Object> reqMap) throws MyException {
        Map<String, Object> resultMap = new HashMap<>();

        reqMap.put("s_id", user.getS_id());

        //列表
        Map<String, Object> pageMap = this.appPage(reqMap);
        //总记录数
        int count = appManagementDao.getPictureCount(reqMap);
        int pageSize = (int) pageMap.get("limit");
        List<Map<String, Object>> list = appManagementDao.getPictureList(reqMap);
        //页数
        this.pageInfo(resultMap, list, count, pageSize);
        resultMap.put("defaultSelectServer", user.getS_id());
        return resultMap;
    }

    /**
     * 轮播图设置_详情列表展示
     *
     * @param reqMap
     * @return
     * @throws MyException
     */
    @Override
    public Map<String, Object> pictureDetailList(User user, Map<String, Object> reqMap) throws MyException {
        Map<String, Object> resultMap = new HashMap<>(16);
        List<Map<String, Object>> list,
                indexPicture,
                personalCenterPicture;
        reqMap.put("s_id", reqMap.get("s_id").toString());
        reqMap.put("user_account", user.getUser_account());
        list = appManagementDao.getPictureDetailList(reqMap, 0);
        indexPicture = appManagementDao.getPictureDetailList(reqMap, 1);
        personalCenterPicture = appManagementDao.getPictureDetailList(reqMap, 2);
        if (list.size() == 0) {
            reqMap.put("s_id", '0');
            list = appManagementDao.getPictureDetailList2(reqMap, 0);
        }

        if (indexPicture.size() == 0) {
            reqMap.put("s_id", '0');
            indexPicture = appManagementDao.getPictureDetailList2(reqMap, 1);
        }

        if (personalCenterPicture.size() == 0) {
            reqMap.put("s_id", '0');
            personalCenterPicture = appManagementDao.getPictureDetailList2(reqMap, 2);
        }
        if (list.size() == 0) {
            for (int i = 0; i < 5; i++) {
                list.add(new HashMap<>());
            }
        }
        resultMap.put("list", list);
        resultMap.put("indexPicture", indexPicture);
        resultMap.put("personalCenterPicture", personalCenterPicture);
        return resultMap;
    }


    /**
     * 查询内部链接列表
     *
     * @param reqMap
     * @param currentUser
     * @return
     * @throws MyException
     */
    @Override
    public List<Map<String, Object>> getInnerHrefPath(Map<String, Object> reqMap, User currentUser) throws MyException {
        return appManagementDao.getInnerHrefPath();
    }

    /**
     * 轮播图设置_获取服务商和游客的列表
     *
     * @param user
     * @param reqMap
     * @return
     */
    @Override
    public List<Map<String, Object>> serverAndTourist(User user, Map<String, Object> reqMap) {
        reqMap.put("s_id", user.getS_id());
        return appManagementDao.serverAndTourist(reqMap);
    }

    /**
     * 轮播图设置_新增轮播图
     *
     * @param reqMap
     * @return
     * @throws MyException
     */
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public int insertPicture(Map<String, Object> reqMap, User currentUser) throws MyException {
        reqMap.put("update_user", currentUser.getUser_name());
        return this.insertPicture(currentUser, reqMap);
    }

    private Map<String, Object> generatePictureParamsHasId(String picturePath, Integer type, Integer sequence, String isInner, String pictureRedirectPath,  String sId, String updateUser) {
        Map<String, Object> params = this.generatePictureParams(picturePath, type, sequence, isInner, pictureRedirectPath, sId, updateUser);
        params.put("id", UuidUtil.get32UUID());
        return params;
    }

    private Map<String, Object> generatePictureParams(String picturePath, Integer type, Integer sequence, String isInner, String pictureRedirectPath, String sId, String updateUser) {
        Map<String, Object> params = new HashMap<>();
        params.put("picturePath", picturePath);
        params.put("sequence", sequence);
        params.put("type", type);
        params.put("is_inner", isInner);
        params.put("pictureRedirectPath", pictureRedirectPath);
        params.put("id", UuidUtil.get32UUID());
        params.put("s_id", sId);
        params.put("update_user", updateUser);
        return params;
    }


    /**
     * 轮播图设置_删除轮播图
     *
     * @param reqMap
     * @return
     */
    @Override
    public int deletePicture(Map<String, Object> reqMap) throws MyException {
        if (!"0".equals(reqMap.get("s_id").toString()) || reqMap.get("inner_delete") != null) {
            return appManagementDao.deletePicture(reqMap);
        }
        return -1;
    }

    /**
     * 轮播图设置_修改轮播图
     *
     * @param reqMap
     * @return
     */
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public int updatePicture(Map<String, Object> reqMap, User currentUser) throws MyException {
        reqMap.put("update_user", currentUser.getUser_name());
        reqMap.put("inner_delete", true);
        this.deletePicture(reqMap);
        return this.insertPicture(currentUser, reqMap);
    }

    private int insertPicture(User currentUser, Map<String, Object> reqMap){

        List<String> picturePathList = (List<String>) reqMap.get("picture_path");
        List<String> pictureRedirectPathList = (List<String>) reqMap.get("picture_redirect_path");
        List<Object> picturePathIsInner = (List<Object>) reqMap.get("picture_path_is_inner");
        int result = 0;
        for (int index = 0; index < picturePathList.size(); ++index) {
            Map<String, Object> params = this.generatePictureParamsHasId(picturePathList.get(index), 0, index,
                    picturePathIsInner.get(index).toString(), pictureRedirectPathList.get(index), currentUser.getS_id(), currentUser.getId());
            result += appManagementDao.insertPicture(params);
        }
        if (reqMap.get("indexPicturePath") != null) {
            Map<String, Object> params = this.generatePictureParamsHasId(reqMap.get("indexPicturePath").toString(), 1,
                    0, reqMap.get("indexPictureIsInner").toString(), reqMap.get("indexPictureRedirectPath").toString(),
                    currentUser.getS_id(), currentUser.getId());
            result += appManagementDao.insertPicture(params);
        }

        if (reqMap.get("personalCenterPicturePath") != null) {
            Map<String, Object> params = this.generatePictureParamsHasId(reqMap.get("personalCenterPicturePath").toString(), 2,
                    0, reqMap.get("personalCenterPictureIsInner").toString(), reqMap.get("personalCenterPictureRedirectPath").toString(),
                    currentUser.getS_id(), currentUser.getId());
            result += appManagementDao.insertPicture(params);
        }
        return result;
    }

    @Override
    public List<Map<String, Object>> getSeverListForRotationChart(Map<String, Object> paraMap) {
        return appManagementDao.getSeverListForRotationChart(paraMap);
    }

    @Override
    public List<Map<String, Object>> getRotationChartDataList(Map<String, Object> paraMap) {
        return appManagementDao.getRotationChartDataList(paraMap);
    }

    @Override
    public List<Map<String, Object>> getRotationChartDataListForServer(Map<String, Object> paraMap) {
        return appManagementDao.getRotationChartDataListForServer(paraMap);
    }

    @Override
    public Map<String, Object> getRotationChartSettingForServer(Map<String, Object> paraMap) {
        return appManagementDao.getRotationChartSettingForServer(paraMap);
    }

    @Override
    public  Map<String, Object> getRotationChartSettingForParentServer(Map<String, Object> paraMap) {
        return appManagementDao.getRotationChartSettingForParentServer(paraMap);
    }

    @Override
    public boolean updateChildrenServerPictureNumByPId(Map<String, Object> paraMap) {
        boolean result = true;
        try {
            appManagementDao.updateChildrenServerPictureNumByPId(paraMap);
        } catch (Exception ex) {
            result = false;
        }
        return result;
    }

    @Override
    public boolean updatePictureDetail(Map<String, Object> paraMap) {
        boolean result = true;
        try {
            appManagementDao.updatePictureDetail(paraMap);
        } catch (Exception ex) {
            result = false;
        }
        return result;
    }

    @Override
    public boolean addPictureDetail(Map<String, Object> paraMap) {
        boolean result = true;
        try {
            paraMap.put("id", UuidUtil.get32UUID());
            appManagementDao.addPictureDetail(paraMap);
        } catch (Exception ex) {
            result = false;
        }
        return result;
    }
}
